package org.dromara.app.controller.app;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import lombok.RequiredArgsConstructor;
import org.dromara.app.domain.bo.AppMessageBo;
import org.dromara.app.domain.vo.AppMessageVo;
import org.dromara.app.service.AppMessageService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;

/**
 * App端消息管理
 *
 * @author ruoyi
 */
@Tag(name = "App端消息管理", description = "App端消息管理相关接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/message")
public class AppMessageController extends BaseController {

    private final AppMessageService appMessageService;

    /**
     * 查询App端消息列表
     */
    @Operation(summary = "查询消息列表", description = "查询App端消息列表")
    @SaCheckPermission("app:message:list")
    @GetMapping("/list")
    public TableDataInfo<AppMessageVo> list(@Parameter(description = "查询条件") @Valid AppMessageBo bo, 
                                            @Parameter(description = "分页参数") @Valid PageQuery pageQuery) {
        return appMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取当前用户的消息列表
     */
    @Operation(summary = "获取我的消息", description = "获取当前用户的消息列表")
    @GetMapping("/myMessages")
    public TableDataInfo<AppMessageVo> getMyMessages(@Parameter(description = "分页参数") @Valid PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        AppMessageBo bo = new AppMessageBo();
        bo.setReceiverId(loginUser.getUserId());
        return appMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取当前用户未读消息数量
     */
    @Operation(summary = "获取未读消息数量", description = "获取当前用户未读消息数量")
    @GetMapping("/unreadCount")
    public R<Long> getUnreadCount() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        Long count = appMessageService.getUnreadCount(loginUser.getUserId());
        return R.ok(count);
    }

    /**
     * 导出App端消息列表
     */
    @Operation(summary = "导出消息列表", description = "导出App端消息列表")
    @SaCheckPermission("app:message:export")
    @Log(title = "App端消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "查询条件") @Valid AppMessageBo bo, HttpServletResponse response) {
        List<AppMessageVo> list = appMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "App端消息", AppMessageVo.class, response);
    }

    /**
     * 获取App端消息详细信息
     */
    @Operation(summary = "获取消息详情", description = "获取App端消息详细信息")
    @SaCheckPermission("app:message:query")
    @GetMapping("/{messageId}")
    public R<AppMessageVo> getInfo(@Parameter(description = "消息ID", required = true) 
                                   @PathVariable @NotNull(message = "消息ID不能为空") @Positive(message = "消息ID必须为正数") Long messageId) {
        return R.ok(appMessageService.queryById(messageId));
    }

    /**
     * 新增App端消息
     */
    @Operation(summary = "新增消息", description = "新增App端消息")
    @SaCheckPermission("app:message:add")
    @Log(title = "App端消息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Parameter(description = "消息信息", required = true) 
                       @Validated(AddGroup.class) @RequestBody @NotNull(message = "消息信息不能为空") AppMessageBo bo) {
        return toAjax(appMessageService.insertByBo(bo));
    }

    /**
     * 修改App端消息
     */
    @Operation(summary = "修改消息", description = "修改App端消息")
    @SaCheckPermission("app:message:edit")
    @Log(title = "App端消息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Parameter(description = "消息信息", required = true) 
                        @Validated(EditGroup.class) @RequestBody @NotNull(message = "消息信息不能为空") AppMessageBo bo) {
        return toAjax(appMessageService.updateByBo(bo));
    }

    /**
     * 删除App端消息
     */
    @Operation(summary = "删除消息", description = "删除App端消息")
    @SaCheckPermission("app:message:remove")
    @Log(title = "App端消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{messageIds}")
    public R<Void> remove(@Parameter(description = "消息ID数组", required = true) 
                          @PathVariable @NotEmpty(message = "消息ID不能为空") Long[] messageIds) {
        return toAjax(appMessageService.deleteWithValidByIds(List.of(messageIds), true));
    }

    /**
     * 标记消息为已读
     */
    @Operation(summary = "标记消息已读", description = "标记指定消息为已读")
    @Log(title = "标记消息已读", businessType = BusinessType.UPDATE)
    @PutMapping("/markRead/{messageId}")
    public R<Void> markRead(@Parameter(description = "消息ID", required = true) 
                            @PathVariable @NotNull(message = "消息ID不能为空") @Positive(message = "消息ID必须为正数") Long messageId) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return toAjax(appMessageService.markAsRead(messageId, loginUser.getUserId()));
    }

    /**
     * 批量标记消息为已读
     */
    @Operation(summary = "批量标记已读", description = "批量标记消息为已读")
    @Log(title = "批量标记消息已读", businessType = BusinessType.UPDATE)
    @PutMapping("/markReadBatch")
    public R<Void> markReadBatch(@Parameter(description = "消息ID数组", required = true) 
                                 @RequestBody @NotEmpty(message = "消息ID列表不能为空") Long[] messageIds) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return toAjax(appMessageService.markAsReadBatch(List.of(messageIds), loginUser.getUserId()));
    }

    /**
     * 标记所有消息为已读
     */
    @Operation(summary = "标记全部已读", description = "标记所有消息为已读")
    @Log(title = "标记所有消息已读", businessType = BusinessType.UPDATE)
    @PutMapping("/markAllRead")
    public R<Void> markAllRead() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return toAjax(appMessageService.markAllAsRead(loginUser.getUserId()));
    }

    /**
     * 发送系统消息
     */
    @Operation(summary = "发送系统消息", description = "发送系统消息")
    @SaCheckPermission("app:message:send")
    @Log(title = "发送系统消息", businessType = BusinessType.INSERT)
    @PostMapping("/sendSystem")
    public R<Void> sendSystemMessage(@Parameter(description = "消息信息", required = true) 
                                     @RequestBody @NotNull(message = "消息信息不能为空") @Valid AppMessageBo bo) {
        return toAjax(appMessageService.sendSystemMessage(bo));
    }

    /**
     * 发送个人消息
     */
    @Operation(summary = "发送个人消息", description = "发送个人消息")
    @Log(title = "发送个人消息", businessType = BusinessType.INSERT)
    @PostMapping("/sendPersonal")
    public R<Void> sendPersonalMessage(@Parameter(description = "消息信息", required = true) 
                                       @RequestBody @NotNull(message = "消息信息不能为空") @Valid AppMessageBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setSenderId(loginUser.getUserId());
        return toAjax(appMessageService.sendPersonalMessage(bo));
    }

    /**
     * 推送消息
     */
    @Operation(summary = "推送消息", description = "推送消息")
    @SaCheckPermission("app:message:push")
    @Log(title = "推送消息", businessType = BusinessType.OTHER)
    @PostMapping("/push")
    public R<Void> pushMessage(@Parameter(description = "消息信息", required = true) 
                               @Validated @RequestBody @NotNull(message = "消息信息不能为空") AppMessageBo bo) {
        return toAjax(appMessageService.pushMessage(bo));
    }

    /**
     * 群发消息
     */
    @Operation(summary = "群发消息", description = "群发消息")
    @SaCheckPermission("app:message:broadcast")
    @Log(title = "群发消息", businessType = BusinessType.INSERT)
    @PostMapping("/broadcast")
    public R<Void> broadcastMessage(@Parameter(description = "消息信息", required = true) 
                                    @Validated @RequestBody @NotNull(message = "消息信息不能为空") AppMessageBo bo) {
        return toAjax(appMessageService.broadcastMessage(bo));
    }
}